package com.kaaction.modules.mer.api;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.mer.service.MerClassifyService;
import com.kaaction.modules.mer.service.SpuService;


@Controller
@RequestMapping(value = "/api/v1/spuApi")
public class SpuApiController extends BaseController{
	
	@Autowired
	MerClassifyService classifyService;
	
	@Autowired
	SpuService spuService;
	
	
	/**
	 * 首页详情
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/spuClassify")
	public void spuClassify(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		Map<String,Object> map = new HashMap<String,Object>();
		//List<Map<String,Object>> listClassify = classifyService.selectClassify();//顶级分类
		//List<Map<String,Object>> listBanner = operationPositionService.selectBanner();//Banner展示
		List<Map<String,Object>> listHome = spuService.selectHome();//首页推荐展示
		//map.put("listClassify", listClassify);
		//map.put("listBanner", listBanner);
		map.put("listHome", listHome);
		super.writeAjaxResponse(response, map);
	}
	 
	/**
	 *子集分类
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/classifySubset")
	public void classifySubset(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");
		super.writeAjaxResponse(response, classifyService.selClassifySubset(id));
	}
	 
	
	/**
	 * 分类列表展示
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/spuClassifyDetail")
	public void spuClassifyDetail(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		String type= request.getParameter("type");
		String name= request.getParameter("name");
		
		int pa = Integer.valueOf(request.getParameter("pageNo"));
		int pageNo = (pa -1)*10;
		int pageSize = 10;
		
		map.put("id", id);
		map.put("type", type);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("name", name);
		
		super.writeAjaxResponse(response, spuService.spuClassifyDetail(map));
	}
	
	/**
	 * 商品详情
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/spuProductDetail")
	public void spuProductDetail(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String id = request.getParameter("id");
		super.writeAjaxResponse(response, spuService.selectProductDetail(id));
	}
	
	
	/**
	 * 商品分类
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/spuProductClassify")
	public void spuProductClassify(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		int pa = Integer.valueOf(request.getParameter("pageNo"));
		int pageNo = (pa -1)*10;
		int pageSize = 10;
		
		map.put("id", id);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		super.writeAjaxResponse(response, spuService.selecProductClassify(map));
	}
	
	/**
	 * 
	 * 标签聚合展示
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/spuTagShow")
	public void spuTagShow(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String tag = request.getParameter("tag");
		int pa = Integer.valueOf(request.getParameter("pageNo"));
		int pageNo = (pa -1)*10;
		int pageSize = 10;
		
		map.put("tag", tag);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		List<Map<String,Object>>  list= spuService.spuTagShow(map);
		int count = spuService.spuTagShowCount(map);
		map.clear();
		map.put("listSpu", list);
		map.put("count", count);
		super.writeAjaxResponse(response,map);
	}
	
}
